package setting

import (
	"../common"
	"../web"
	"encoding/json"
	"strconv"
	"strings"
)

type SetFailureObj struct {
	common.BaseHandler
}

type FailureQueryPageObj struct {
	common.BaseHandler
}

func (self *SetFailureObj) Prepare() {
	failures := *common.FetchAllRowsPtr(
		`SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY FAILCODE),FAILCODE,ISNULL(FAILDESC,''),
					ISNULL(DESCCHN,''),ISNULL(FAILTYPE,'') 
           FROM SFC_FAILURE(NOLOCK)`,
		self.Usr.Conn, new(common.FailureInfo))

	curr_id := 1
	iRtnData := *common.FetchOneRowPtr(
		`SELECT COUNT(FAILCODE)
           FROM SFC_FAILURE(NOLOCK)`,
		self.Usr.Conn, new(common.RecCount))
	recnum := iRtnData.(common.RecCount).Number
	total_id := recnum / 10
	if recnum%10 > 0 {
		total_id = total_id + 1
	}

	self.TmplData["failures"] = failures
	self.TmplData["curr_page"] = curr_id
	self.TmplData["total_page"] = total_id
}

func (self *SetFailureObj) ParameterCheck() bool {
	c_code := strings.TrimSpace(self.Ctx.Params["failcode"])
	c_type := strings.TrimSpace(self.Ctx.Params["failtype"])
	c_desc := strings.TrimSpace(self.Ctx.Params["faildesc"])
	c_dchn := strings.TrimSpace(self.Ctx.Params["descchn"])
	if c_code == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the failcode.")
		return false
	}

	self.Sqlparas = append(self.Sqlparas, c_code)
	self.Sqlparas = append(self.Sqlparas, c_desc)
	self.Sqlparas = append(self.Sqlparas, c_dchn)
	self.Sqlparas = append(self.Sqlparas, c_type)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)
	self.Flashparas = []interface{}{strings.ToUpper(c_code)}

	return true
}

func (self *SetFailureObj) SqlExecute() bool {
	spRtnData := *common.FetchOneRowPtr(
		`sp_SetFailure ?, ?, ?, ?, ?`,
		self.Usr.Conn, new(common.SPReturn), self.Sqlparas...)
	if spRtnData.(common.SPReturn).Flag > 0 {
		self.TmplData["error"] = common.GetTextSP(self.Lang, spRtnData)
		return false
	}
	return true
}

func FailureSetHandler(ctx *web.Context) {
	self := new(SetFailureObj)
	self.Ctx = ctx
	self.Url = "/setfailure"
	self.Html = "setfailure.html"
	self.Rightlist = []string{"setfailure"}
	self.Flashstr = "Set Failure: [ %v ] finished."
	self.Do(self)
}

func (self *FailureQueryPageObj) ParameterCheck() bool {
	s_page := self.Ctx.Params["pageid"]
	pageid, err := strconv.Atoi(s_page)
	if err != nil {
		return false
	}

	idx := 10 * (pageid - 1)
	self.Sqlparas = append(self.Sqlparas, idx)

	return true
}

func (self *FailureQueryPageObj) SqlQueryExecute() {
	failures := *common.FetchAllRowsPtr(
		`SELECT TOP 10 A.AUTOID, A.FAILCODE, A.FAILDESC, A.DESCCHN, A.FAILTYPE
           FROM
                (
					SELECT ROW_NUMBER() OVER(ORDER BY FAILCODE) AS AUTOID,FAILCODE,
							ISNULL(FAILDESC,'') AS FAILDESC,ISNULL(DESCCHN,'') AS DESCCHN,
							ISNULL(FAILTYPE,'') AS FAILTYPE
			          FROM SFC_FAILURE(NOLOCK)
			    ) A
		  WHERE A.AUTOID > ?`,
		self.Usr.Conn, new(common.FailureInfo), self.Sqlparas...)

	dict_js, _ := json.Marshal(map[string]interface{}{"failures": failures, "lang": self.Lang})
	self.Ctx.ResponseWriter.Write(dict_js)
}

func FailureSetPageHandler(ctx *web.Context) {
	self := new(FailureQueryPageObj)
	self.Ctx = ctx
	self.DoJsonQuery(self)
}
